package com.itheima.info.manager.Util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class AgeUtil {

    private AgeUtil() {
    }

    //接收出生日期字符串,返回年龄
    public static String getAge(String birthday){
        //调用simpledateformat方法将字符串转换成Date类型
        //时间毫秒值 = 当前毫秒值 - 生日毫秒值
        // age= 时间毫秒值  /(1000*60*60*24) /365

        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date birthdayStr = sdf.parse(birthday);

            Date nowdate = new Date();
            long time = nowdate.getTime() - birthdayStr.getTime();
            long age = time /(1000L * 60 * 60 * 24) / 365;
            return  age + "";
        }catch(Exception e){
            return "-1";
        }

    }

}
